package cn.kai.org.service.impl;

import cn.kai.basic.config.BussinessException;
import cn.kai.basic.service.impl.BaseServiceImpl;
import cn.kai.basic.util.MD5Utils;
import cn.kai.basic.util.StrUtils;
import cn.kai.org.domain.Employee;
import cn.kai.org.mapper.EmployeeMapper;
import cn.kai.org.service.IEmployeeServiceI;
import cn.kai.user.domain.Logininfo;
import cn.kai.user.mapper.LogininfoMapper;
import cn.kai.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeServiceI {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoService;

//    @Autowired
//    private EmployeeMapper employeeMapper;
//
    @Override
    @Transactional
    public void add(Employee employee) {
        verifyParam(employee);
        Logininfo logininfo = empToLoginfo(employee);
        logininfoService.add(logininfo);
        Logininfo byPhone = logininfoService.findByPhone(employee.getPhone());
        employee.setLogininfoId(byPhone.getId());
        employeeMapper.add(employee);
        //初始化关连表数据
        initRelationTable(employee);
    }

    //修改关联表
    private void initRelationTable(Employee employee) {
        List<Long> roles = employee.getRoles();
        employeeMapper.removeEmployeeRole(employee.getId()); // 删除员工角色中间表数据
        employeeMapper.saveEmployeeRole(employee.getId(),roles);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        verifyParam(employee);
        Logininfo logininfo = empToLoginfo(employee);
        if(logininfo.getId() == null){
            logininfoService.add(logininfo);
            Logininfo byPhone = logininfoService.findByPhone(employee.getPhone());
            employee.setLogininfoId(byPhone.getId());
        }else {
            logininfoService.update(logininfo);
        }
        employeeMapper.update(employee);
        //初始化关连表数据
        initRelationTable(employee);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        if (id == null){
            throw new BussinessException("id不能为空");
        }
        Employee byId = employeeMapper.findById(id);
        if (byId != null){
            Long logininfoId = byId.getLogininfoId();
            logininfoService.delete(logininfoId);
        }
        employeeMapper.delete(id);
        employeeMapper.removeEmployeeRole(id);
    }

    private void verifyParam(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String password = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(password);
        Logininfo byPhone = logininfoService.findByPhone(employee.getPhone());
        Logininfo byEmail = logininfoService.findByEmail(employee.getEmail());
        if(byEmail != null){
            Long id = byEmail.getId();
            employee.setLogininfoId(id);
        }
        if(byPhone != null){
            Long id = byPhone.getId();
            employee.setLogininfoId(id);
        }
    }

    //封装一个logininfo对象
    private Logininfo empToLoginfo(Employee employee){
        Logininfo logininfo = new Logininfo();
        if (employee != null){
            logininfo.setId(employee.getLogininfoId());
            logininfo.setEmail(employee.getEmail());
            logininfo.setPhone(employee.getPhone());
            logininfo.setSalt(employee.getSalt());
            logininfo.setPassword(employee.getPassword());
            logininfo.setUsername(employee.getUsername());
            logininfo.setType(0);
        }
        return logininfo;
    }

    @Override
    public Employee findByLogininfoId(Long id) {
        return employeeMapper.findByLogininfoId(id);
    }

    @Override
    public List<Employee> list(Long shopId) {
        return employeeMapper.list(shopId);
    }
//

//
//    @Override
//    public List<Employee> findAll() {
//        return employeeMapper.findAll();
//    }
//
//    @Override
//    public Employee findById(Long id) {
//        return employeeMapper.findById(id);
//    }
//
//    @Override
//    public PageList<Employee> queryByPage(EmployeeQuery query) {
//        // 查询条数
//        Integer totals = employeeMapper.queryByCount(query);
//        if (null == totals || 0 == totals){
//            return new PageList<>(0,new ArrayList<>());
//        }
//        // 查询数据
//        List<Employee> employees = employeeMapper.queryByPage(query);
//        //方到返回里面
//        return new PageList<>(totals,employees);
//    }
//
//    @Override
//    @Transactional
//    public void batchDel(List<Long> ids) {
//        if (ids == null){
//            throw new BussinessException("ids不能为空");
//        }
//        employeeMapper.batchDel(ids);
//    }
}
